package com.slt.seata.demo.s2.controller;

import com.alibaba.fastjson.JSONObject;
import com.slt.seata.demo.s2.domain.SysUser;
import com.slt.seata.demo.s2.feigen.SysOperLogFeign;
import com.slt.seata.demo.s2.mapper.SysUserMapper;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class MyController {

    @Autowired
    private SysOperLogFeign sysOperLogFeign;

    @Autowired
    private SysUserMapper sysUserMapper;


    @GetMapping("/saveTest")
    @GlobalTransactional
    public Object save(String loginName) {
        if(loginName==null){
            loginName="test"+System.currentTimeMillis();
        }
        SysUser sysUser=new SysUser();
        sysUser.setLoginName(loginName);
        sysUser.setUserName(loginName);
        sysUserMapper.insert(sysUser);
//        if(true){
//            throw new RuntimeException("插入用户异常");
//        }
        log.info("user 服务插入完成。");
        Object o=sysOperLogFeign.save("system","创建登录用户："+loginName);
        log.info("log 服务插入完成。");
//        if(true){
//            throw new RuntimeException("其他异常");
//        }

        return JSONObject.toJSONString(o);
    }
}
